using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace LabyrinthProject
{
    class MiniMap : IDrawable
    {
        #region fields
        private SpriteBatch sprite;

        private Texture2D playerMiniMap;
        private Texture2D mapEdgeTexture;
        private Texture2D whiteTexture;

        private Labyrinth labyrinth;

        Player player1;
        Player player2;
        #endregion

        #region initialization
        //dodani su playeri jer želim da se iscrtava i mjesto igrača na mapi - ne mjenjati to - Marko
        public MiniMap(Labyrinth labyrinth, Texture2D playerMiniMap, Texture2D mapEdgeTexture, Texture2D whiteTexture, Player player1, Player player2)
        {
            this.playerMiniMap = playerMiniMap;
            this.mapEdgeTexture = mapEdgeTexture;
            this.whiteTexture = whiteTexture;
            this.labyrinth = labyrinth;

            this.player1 = player1;
            this.player2 = player2;

            this.sprite = new SpriteBatch(LabyrinthGame.GDevice);

        }
        #endregion

        #region methods
        public void Draw(Camera camera)
        {		  
            if (DoDraw(camera))
            {                
                LabyrinthGame.GDevice.Viewport = camera.Viewport;

                Rectangle screenRectangleMap;
                Rectangle screenRectangleEdge;

                float rotation = -MathHelper.ToRadians(180) + (float)Math.Atan2(camera.Player.Direction.X, camera.Player.Direction.Z);

                if (camera.Viewport.X == 0)
                {
                    screenRectangleMap = new Rectangle(100, 100, 100, 100);
                    screenRectangleEdge = new Rectangle(20, 20, 160, 160);
                }
                else
                {
                    screenRectangleMap = new Rectangle(camera.Viewport.Width - 100, 100, 100, 100);
                    screenRectangleEdge = new Rectangle(camera.Viewport.Width - 180, 20, 160, 160);
                }

                //other player
                Player otherPlayer;
                if (camera.Player == player1)
                    otherPlayer = player2;
                else
                    otherPlayer = player1;

                DrawEdge(screenRectangleEdge);
                DrawLabyrinth(rotation, screenRectangleMap);
                DrawStart(rotation, screenRectangleMap, screenRectangleEdge, Color.Yellow);
                DrawEnd(rotation, screenRectangleMap, screenRectangleEdge, Color.LightGreen);

                DrawPlayer(camera.Player, rotation, screenRectangleMap, screenRectangleEdge, Color.Blue);
                DrawPlayer(otherPlayer, rotation, screenRectangleMap, screenRectangleEdge, Color.Blue);
            }
		}

        private bool DoDraw(Camera camera)
        {
            for (int i = 0; i < camera.Player.PowerUpList.Count; i++)
            {
                if (camera.Player.PowerUpList[i].TypeOfPowerUp == PowerUpType.Kompas)
                    if (camera.Player.PowerUpList[i].LifeTime >= 0)
                        return true;
            }

            return false;
        }

        private void DrawEdge(Rectangle screenRectangleEdge)
        {
            sprite.Begin(SpriteSortMode.Texture, BlendState.AlphaBlend);
            sprite.Draw(mapEdgeTexture, screenRectangleEdge, null, new Color (new Vector4(1,1,1,0.5f)), 0, new Vector2(0, 0), SpriteEffects.None, 0);
            sprite.End();
        }

        private void DrawLabyrinth(float rotation, Rectangle screenRectangleMap)
        {

            sprite.Begin(SpriteSortMode.Texture, BlendState.Additive, SamplerState.PointWrap, DepthStencilState.None, RasterizerState.CullCounterClockwise);
            sprite.Draw(labyrinth.LabyrinthTexture, screenRectangleMap, null, Color.White, rotation, new Vector2(labyrinth.LabyrinthTexture.Width / 2f, labyrinth.LabyrinthTexture.Height / 2f), SpriteEffects.None, 0);

            sprite.End();
        }

        private void DrawPlayer(Player player,float rotation, Rectangle screenRectangleMap, Rectangle screenRectangleEdge, Color color)
        {
            Vector2 origin = new Vector2(screenRectangleMap.X, screenRectangleMap.Y);
            Vector2 pos = new Vector2(screenRectangleMap.X - 48 + (player.Position.X / 1000f) * 100 / labyrinth.Width, 52 + (player.Position.Z / 1000f) * 100 / labyrinth.Height);
            Vector2 temp = pos - origin;
            Vector2 result = new Vector2(temp.X * (float)System.Math.Cos(rotation) - temp.Y * (float)System.Math.Sin(rotation), temp.X * (float)System.Math.Sin(rotation) + temp.Y * (float)System.Math.Cos(rotation));
            result = result + origin;

            sprite.Begin(SpriteSortMode.Texture, BlendState.NonPremultiplied);
            sprite.Draw(playerMiniMap, new Vector2(result.X, result.Y), null, color, 0, new Vector2(playerMiniMap.Width / 2f, playerMiniMap.Height / 2f), 0.02f, SpriteEffects.None, 0);
            sprite.End();
        }

        private void DrawStart(float rotation, Rectangle screenRectangleMap, Rectangle screenRectangleEdge, Color color)
        {
            Vector2 origin = new Vector2(screenRectangleMap.X, screenRectangleMap.Y);
            Vector2 pos = new Vector2(screenRectangleMap.X - 48 + (-1) * 100 / labyrinth.Width, 52 + ((1000 + 1000 * (labyrinth.Width / 2f - 1)) / 1000f) * 100 / labyrinth.Height);
            Vector2 temp = pos - origin;
            Vector2 result = new Vector2(temp.X * (float)System.Math.Cos(rotation) - temp.Y * (float)System.Math.Sin(rotation), temp.X * (float)System.Math.Sin(rotation) + temp.Y * (float)System.Math.Cos(rotation));
            result = result + origin;

            sprite.Begin(SpriteSortMode.Texture, BlendState.NonPremultiplied, SamplerState.PointWrap, DepthStencilState.None, RasterizerState.CullCounterClockwise);
            sprite.Draw(whiteTexture, new Vector2(result.X, result.Y), null, color, rotation, new Vector2(whiteTexture.Width / 2f, whiteTexture.Height / 2f), 0.5f, SpriteEffects.None, 0);
            sprite.End();
        }

        private void DrawEnd(float rotation, Rectangle screenRectangleMap, Rectangle screenRectangleEdge, Color color)
        {

            Vector2 origin = new Vector2(screenRectangleMap.X, screenRectangleMap.Y);
            Vector2 pos = new Vector2(screenRectangleMap.X - 48 + (labyrinth.Height) * 100 / labyrinth.Width, 52 + ((1000 + 1000 * (labyrinth.Width / 2f - 1)) / 1000f) * 100 / labyrinth.Height);
            Vector2 temp = pos - origin;
            Vector2 result = new Vector2(temp.X * (float)System.Math.Cos(rotation) - temp.Y * (float)System.Math.Sin(rotation), temp.X * (float)System.Math.Sin(rotation) + temp.Y * (float)System.Math.Cos(rotation));
            result = result + origin;

            sprite.Begin(SpriteSortMode.Texture, BlendState.NonPremultiplied, SamplerState.PointWrap, DepthStencilState.None, RasterizerState.CullCounterClockwise);
            sprite.Draw(whiteTexture, new Vector2(result.X, result.Y), null, color, rotation, new Vector2(whiteTexture.Width / 2f, whiteTexture.Height / 2f), 0.5f, SpriteEffects.None, 0);
            sprite.End();
        }
        #endregion
    }
}
